<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 14 Zygote进程解析 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 14 Zygote进程解析">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 14 Zygote进程解析">
<meta property="og:url" content="http://longzhiye.top/2024/03/02/2024-03-02/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 14 Zygote进程解析">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://longzhiye.top/images/20240302/20240302001.png">
<meta property="og:image" content="http://longzhiye.top/images/20240302/20240302001.png">
<meta property="article:published_time" content="2024-03-01T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.925Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="framework">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://longzhiye.top/images/20240302/20240302001.png">

  <meta name="keywords" content=",Android,Linux,framework">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2024/03/02/2024-03-02/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2024/03/02/2024-03-02/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-16.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 14 Zygote进程解析</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2024-03-02 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2024/03/02/2024-03-02/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 14 Zygote进程解析&url=http://longzhiye.top/2024/03/02/2024-03-02/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 14 Zygote进程解析&url=http://longzhiye.top/2024/03/02/2024-03-02/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2024/03/02/2024-03-02/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2024/03/02/2024-03-02/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2024/03/02/2024-03-02/&title=Android 14 Zygote进程解析" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 14 Zygote进程解析&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2024/03/02/2024-03-02/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2024/03/02/2024-03-02/&text=Android 14 Zygote进程解析" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>init进程启动后，最重要的一个进程就是Zygote进程,Zygote是所有应用的鼻祖。SystemServer和其他所有Dalivik虚拟机进程都是由Zygote fork而来。<br>Zygote进程由app_process启动，Zygote是一个C/S模型，Zygote进程作为服务端，其他进程作为客户端向它发出“孵化-fork”请求，而Zygote接收到这个请求后就“孵化-fork”出一个新的进程。<br>由于Zygote进程在启动时会创建Java虚拟机，因此通过fork而创建的应用程序进程和SystemServer进程可以在内部获取一个Java虚拟机的实例拷贝。<br>架构图</p>
<p><img src="/images/20240302/20240302001.png"></p>
<h3 id="Zygote是如何启动的"><a href="#Zygote是如何启动的" class="headerlink" title="Zygote是如何启动的"></a>Zygote是如何启动的</h3><p>init进程启动后，会解析init.rc文件，然后创建和加载service字段指定的进程。zygote进程就是以这种方式，被init进程加载的。<br><strong>system/core/rootdir/init.rc</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import /system/etc/init/hw/init.$&#123;ro.zygote&#125;.rc</span><br></pre></td></tr></table></figure>
<p>其中${ro.zygote} 由各个厂家使用，现在的主流厂家基本使用zygote64_32，因此，我们的rc文件为 init.zygote64_32.r。它会启动两个zygote进程（名为zygote和zygote——secondary），对应的执行程序分别是app_prcocess64(主模式)、app_process32。<br><strong>init.zygote64_32.rc</strong><br><strong>system/core/rootdir/init.zygote64_32.rc</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygote</span><br><span class="line">    class main</span><br><span class="line">    priority <span class="number">-20</span></span><br><span class="line">    user root <span class="comment">//用户为root</span></span><br><span class="line">    group root readproc reserved_disk <span class="comment">//访问组支持root readproc reserved_disk</span></span><br><span class="line">    socket zygote stream <span class="number">660</span> root system <span class="comment">//创建一个socket，名字叫zygote，以tcp形式，可以在/dev/socket 中看到一个 zygote的socket</span></span><br><span class="line">    socket usap_pool_primary stream <span class="number">660</span> root system</span><br><span class="line">    onrestart exec_background - system system -- /system/bin/vdc volume abort_fuse</span><br><span class="line">    onrestart write /sys/power/state on</span><br><span class="line">     onrestart restart audioserver</span><br><span class="line">     onrestart restart cameraserver</span><br><span class="line">     onrestart restart media</span><br><span class="line">     onrestart restart netd</span><br><span class="line">     onrestart restart wificond</span><br><span class="line">     task_profiles ProcessCapacityHigh MaxPerformance</span><br><span class="line">     critical window=$&#123;zygote.critical_window.minute:-off&#125; target=zygote-fatal</span><br><span class="line"> </span><br><span class="line"> service zygote_secondary /system/bin/app_process32 -Xzygote /system/bin --zygote --socket-name=zygote_secondary --enable-lazy-preload</span><br><span class="line">     class main</span><br><span class="line">     priority <span class="number">-20</span></span><br><span class="line">     user root</span><br><span class="line">     group root readproc reserved_disk</span><br><span class="line">     socket zygote_secondary stream <span class="number">660</span> root system</span><br><span class="line">     socket usap_pool_secondary stream <span class="number">660</span> root system</span><br><span class="line">     onrestart restart zygote</span><br><span class="line">     task_profiles ProcessCapacityHigh MaxPerformance</span><br></pre></td></tr></table></figure>
<p>从上面一段代码可以看出：<br>第一个Zygote进程：<br>进程名：zygote<br>进程通过 /system/bin/app_process64来启动<br>启动参数： -Xzygote<br>/system/bin<br>–zygote<br>–start-system-server<br>–socket-name=zygote<br>socket的名称：zygote<br>第二个Zygote进程：<br>进程名：zygote_secondary<br>进程通过 /system/bin/app_process32来启动<br>启动参数： -Xzygote<br>/system/bin<br>–zygote<br>–socket-name=zygote_secondary<br>–enable-lazy-preload<br>socket的名称：zygote_secondary<br>对应的代码入口为：frameworks/base/cmds/app_process/app_main.cpp</p>
<h3 id="Zygote启动后做了什么"><a href="#Zygote启动后做了什么" class="headerlink" title="Zygote启动后做了什么"></a>Zygote启动后做了什么</h3><p>init进程通过init.zygote64_32.rc来调用/system/bin/app_process64 来启动zygote进程，入口app_main.cpp<br><strong>frameworks/base/cmds/app_process/app_main.cpp</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span>* <span class="type">const</span> argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* </span></span><br><span class="line"><span class="comment">zygote传入的参数是：</span></span><br><span class="line"><span class="comment">        -Xzygote</span></span><br><span class="line"><span class="comment">        /system/bin</span></span><br><span class="line"><span class="comment">        --zygote</span></span><br><span class="line"><span class="comment">        --start-system-server</span></span><br><span class="line"><span class="comment">        --socket-name=zygote</span></span><br><span class="line"><span class="comment">    </span></span><br><span class="line"><span class="comment">zygote_secondary传入的参数是：</span></span><br><span class="line"><span class="comment">        -Xzygote</span></span><br><span class="line"><span class="comment">        /system/bin</span></span><br><span class="line"><span class="comment">        --zygote</span></span><br><span class="line"><span class="comment">        --socket-name=zygote_secondary</span></span><br><span class="line"><span class="comment">        --enable-lazy-preload  </span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    </span><br><span class="line">    ......</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// Parse runtime arguments.  Stop at first unrecognized option.</span></span><br><span class="line">    <span class="type">bool</span> zygote = <span class="literal">false</span>;</span><br><span class="line">    <span class="type">bool</span> startSystemServer = <span class="literal">false</span>;</span><br><span class="line">    <span class="type">bool</span> application = <span class="literal">false</span>;</span><br><span class="line">    String8 niceName;</span><br><span class="line">    String8 className;</span><br><span class="line"> </span><br><span class="line">    ++i;  <span class="comment">// Skip unused &quot;parent dir&quot; argument.</span></span><br><span class="line">    <span class="keyword">while</span> (i &lt; argc) &#123;</span><br><span class="line">        <span class="type">const</span> <span class="type">char</span>* arg = argv[i++];</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">strcmp</span>(arg, <span class="string">&quot;--zygote&quot;</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">            zygote = <span class="literal">true</span>;</span><br><span class="line">            <span class="comment">//对于64位系统nice_name为zygote64，32位系统为zygote</span></span><br><span class="line">            niceName = ZYGOTE_NICE_NAME;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">strcmp</span>(arg, <span class="string">&quot;--start-system-server&quot;</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">//是否需要启动system server</span></span><br><span class="line">            startSystemServer = <span class="literal">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">strcmp</span>(arg, <span class="string">&quot;--application&quot;</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">//启动进入独立的程序模式</span></span><br><span class="line">            application = <span class="literal">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">strncmp</span>(arg, <span class="string">&quot;--nice-name=&quot;</span>, <span class="number">12</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">//niceName为当前进程别名，区别abi型号</span></span><br><span class="line">            niceName.setTo(arg + <span class="number">12</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">strncmp</span>(arg, <span class="string">&quot;--&quot;</span>, <span class="number">2</span>) != <span class="number">0</span>) &#123;</span><br><span class="line">            className.setTo(arg);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            --i;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    Vector&lt;String8&gt; args;</span><br><span class="line">    <span class="keyword">if</span> (!className.isEmpty()) &#123;<span class="comment">//className不为空，说明是application启动模式</span></span><br><span class="line">        <span class="comment">// We&#x27;re not in zygote mode, the only argument we need to pass</span></span><br><span class="line">        <span class="comment">// to RuntimeInit is the application argument.</span></span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">        <span class="comment">// The Remainder of args get passed to startup class main(). Make</span></span><br><span class="line">        <span class="comment">// copies of them before we overwrite them with the process name.</span></span><br><span class="line">        args.add(application ? String8(<span class="string">&quot;application&quot;</span>) : String8(<span class="string">&quot;tool&quot;</span>));</span><br><span class="line">        runtime.setClassNameAndArgs(className, argc - i, argv + i);</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">if</span> (!LOG_NDEBUG) &#123;</span><br><span class="line">          String8 restOfArgs;</span><br><span class="line">          <span class="type">char</span>* <span class="type">const</span>* argv_new = argv + i;</span><br><span class="line">          <span class="type">int</span> argc_new = argc - i;</span><br><span class="line">          <span class="keyword">for</span> (<span class="type">int</span> k = <span class="number">0</span>; k &lt; argc_new; ++k) &#123;</span><br><span class="line">            restOfArgs.append(<span class="string">&quot;\&quot;&quot;</span>);</span><br><span class="line">            restOfArgs.append(argv_new[k]);</span><br><span class="line">            restOfArgs.append(<span class="string">&quot;\&quot; &quot;</span>);</span><br><span class="line">          &#125;</span><br><span class="line">          ALOGV(<span class="string">&quot;Class name = %s, args = %s&quot;</span>, className.<span class="built_in">string</span>(), restOfArgs.<span class="built_in">string</span>());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// We&#x27;re in zygote mode.</span></span><br><span class="line">        <span class="comment">//进入zygote模式</span></span><br><span class="line">        <span class="comment">//新建Dalvik的缓存目录：/data/dalvik-cache</span></span><br><span class="line">        maybeCreateDalvikCache();</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">if</span> (startSystemServer) &#123;</span><br><span class="line">            <span class="comment">//添加start-system-server参数</span></span><br><span class="line">            args.add(String8(<span class="string">&quot;start-system-server&quot;</span>));</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="type">char</span> prop[PROP_VALUE_MAX];</span><br><span class="line">        <span class="keyword">if</span> (property_get(ABI_LIST_PROPERTY, prop, <span class="literal">NULL</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">            LOG_ALWAYS_FATAL(<span class="string">&quot;app_process: Unable to determine ABI list from property %s.&quot;</span>,</span><br><span class="line">                ABI_LIST_PROPERTY);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">11</span>;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        String8 <span class="title function_">abiFlag</span><span class="params">(<span class="string">&quot;--abi-list=&quot;</span>)</span>;</span><br><span class="line">        abiFlag.append(prop);</span><br><span class="line">        <span class="comment">//添加--abi-list=参数</span></span><br><span class="line">        args.add(abiFlag);</span><br><span class="line"> </span><br><span class="line">        <span class="comment">// In zygote mode, pass all remaining arguments to the zygote</span></span><br><span class="line">        <span class="comment">// main() method.</span></span><br><span class="line">        <span class="keyword">for</span> (; i &lt; argc; ++i) &#123;</span><br><span class="line">            <span class="comment">//将剩下的参数加入args</span></span><br><span class="line">            args.add(String8(argv[i]));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//设置一个“好听的昵称” zygote\zygote64，之前的名称是app_process</span></span><br><span class="line">    <span class="keyword">if</span> (!niceName.isEmpty()) &#123;</span><br><span class="line">        runtime.setArgv0(niceName.<span class="built_in">string</span>(), <span class="literal">true</span> <span class="comment">/* setProcName */</span>);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">if</span> (zygote) &#123;</span><br><span class="line">        <span class="comment">//如果是zygote启动模式，则加载ZygoteInit</span></span><br><span class="line">        runtime.start(<span class="string">&quot;com.android.internal.os.ZygoteInit&quot;</span>, args, zygote);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (className) &#123;</span><br><span class="line">        <span class="comment">//如果是application启动模式，则加载RuntimeInit</span></span><br><span class="line">        runtime.start(<span class="string">&quot;com.android.internal.os.RuntimeInit&quot;</span>, args, zygote);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//没有指定类名或zygote，参数错误</span></span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">&quot;Error: no class name or --zygote supplied.\n&quot;</span>);</span><br><span class="line">        app_usage();</span><br><span class="line">        LOG_ALWAYS_FATAL(<span class="string">&quot;app_process: no class name or --zygote supplied.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Zygote本身是一个Native的应用程序，刚开始的进程名称为“app_process”，运行过程中，通过调用setArgv0将名字改为zygote 或者 zygote64(根据操作系统而来)，最后通过runtime的start()方法来真正的加载虚拟机并进入JAVA世界。<br>在app_main.cpp的最后调用了runtime.start()方法，runtime是AppRuntime的对象，AppRuntime是AndroidRuntime的子类，<br><strong>进入到AndroidRuntime.cpp#start()</strong><br><strong>frameworks/base/core/jni/AndroidRuntime.cpp</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">AndroidRuntime::start</span><span class="params">(<span class="type">const</span> <span class="type">char</span>* className, <span class="type">const</span> Vector&lt;String8&gt;&amp; options, <span class="type">bool</span> zygote)</span></span><br><span class="line">&#123;</span><br><span class="line">    ALOGD(<span class="string">&quot;&gt;&gt;&gt;&gt;&gt;&gt; START %s uid %d &lt;&lt;&lt;&lt;&lt;&lt;\n&quot;</span>,</span><br><span class="line">            className != <span class="literal">NULL</span> ? className : <span class="string">&quot;(unknown)&quot;</span>, getuid());</span><br><span class="line"> </span><br><span class="line">    <span class="type">static</span> <span class="type">const</span> String8 <span class="title function_">startSystemServer</span><span class="params">(<span class="string">&quot;start-system-server&quot;</span>)</span>;</span><br><span class="line">    <span class="comment">// Whether this is the primary zygote, meaning the zygote which will fork system server.</span></span><br><span class="line">    <span class="type">bool</span> primary_zygote = <span class="literal">false</span>;</span><br><span class="line"> </span><br><span class="line">    ......</span><br><span class="line">        </span><br><span class="line">    <span class="comment">/* start the virtual machine */</span></span><br><span class="line">    JniInvocation jni_invocation;</span><br><span class="line">    jni_invocation.Init(<span class="literal">NULL</span>);</span><br><span class="line">    JNIEnv* env;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">//启动虚拟机，主要是关于虚拟机参数的设置</span></span><br><span class="line">    <span class="keyword">if</span> (startVm(&amp;mJavaVM, &amp;env, zygote, primary_zygote) != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    onVmCreated(env);<span class="comment">//空函数，没有任何实现</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Register android functions.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//注册JNI方法</span></span><br><span class="line">    <span class="keyword">if</span> (startReg(env) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        ALOGE(<span class="string">&quot;Unable to register all android natives\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * We want to call main() with a String array with arguments in it.</span></span><br><span class="line"><span class="comment">     * At present we have two arguments, the class name and an option string.</span></span><br><span class="line"><span class="comment">     * Create an array to hold them.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    jclass stringClass;</span><br><span class="line">    jobjectArray strArray;</span><br><span class="line">    jstring classNameStr;</span><br><span class="line"> </span><br><span class="line">    stringClass = env-&gt;FindClass(<span class="string">&quot;java/lang/String&quot;</span>);</span><br><span class="line">    assert(stringClass != <span class="literal">NULL</span>);</span><br><span class="line">    <span class="comment">//等价 strArray[0] = &quot;com.android.internal.os.ZygoteInit&quot;</span></span><br><span class="line">    strArray = env-&gt;NewObjectArray(options.size() + <span class="number">1</span>, stringClass, <span class="literal">NULL</span>);</span><br><span class="line">    assert(strArray != <span class="literal">NULL</span>);</span><br><span class="line">    classNameStr = env-&gt;NewStringUTF(className);</span><br><span class="line">    assert(classNameStr != <span class="literal">NULL</span>);</span><br><span class="line">    env-&gt;SetObjectArrayElement(strArray, <span class="number">0</span>, classNameStr);</span><br><span class="line"> </span><br><span class="line">    <span class="comment">//strArray[1]=&quot;start-system-server&quot;;</span></span><br><span class="line">    <span class="comment">//strArray[2]=&quot;--abi-list=xxx&quot;;</span></span><br><span class="line">    <span class="comment">//其中xxx为系统响应的cpu架构类型，比如arm64-v8a</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">size_t</span> i = <span class="number">0</span>; i &lt; options.size(); ++i) &#123;</span><br><span class="line">        jstring optionsStr = env-&gt;NewStringUTF(options.itemAt(i).<span class="built_in">string</span>());</span><br><span class="line">        assert(optionsStr != <span class="literal">NULL</span>);</span><br><span class="line">        env-&gt;SetObjectArrayElement(strArray, i + <span class="number">1</span>, optionsStr);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Start VM.  This thread becomes the main thread of the VM, and will</span></span><br><span class="line"><span class="comment">     * not return until the VM exits.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//将&quot;com.android.internal.os.ZygoteInit&quot;转换为&quot;com/android/internal/os/ZygoteInit&quot;</span></span><br><span class="line">    <span class="type">char</span>* slashClassName = toSlashClassName(className != <span class="literal">NULL</span> ? className : <span class="string">&quot;&quot;</span>);</span><br><span class="line">    jclass startClass = env-&gt;FindClass(slashClassName);<span class="comment">//找到Zygoteinit</span></span><br><span class="line">    <span class="keyword">if</span> (startClass == <span class="literal">NULL</span>) &#123;</span><br><span class="line">        ALOGE(<span class="string">&quot;JavaVM unable to locate class &#x27;%s&#x27;\n&quot;</span>, slashClassName);</span><br><span class="line">        <span class="comment">/* keep going */</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//找到这个类后，继续找成员函数main方法的Method ID</span></span><br><span class="line">        jmethodID startMeth = env-&gt;GetStaticMethodID(startClass, <span class="string">&quot;main&quot;</span>,</span><br><span class="line">            <span class="string">&quot;([Ljava/lang/String;)V&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span> (startMeth == <span class="literal">NULL</span>) &#123;</span><br><span class="line">            ALOGE(<span class="string">&quot;JavaVM unable to find main() in &#x27;%s&#x27;\n&quot;</span>, className);</span><br><span class="line">            <span class="comment">/* keep going */</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//通过反射调用ZygoteInit.main()方法</span></span><br><span class="line">            env-&gt;CallStaticVoidMethod(startClass, startMeth, strArray);</span><br><span class="line"> </span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 0</span></span><br><span class="line">            <span class="keyword">if</span> (env-&gt;ExceptionCheck())</span><br><span class="line">                threadExitUncaughtException(env);</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//释放相应对象的内存空间</span></span><br><span class="line">    <span class="built_in">free</span>(slashClassName);</span><br><span class="line"> </span><br><span class="line">    ALOGD(<span class="string">&quot;Shutting down VM\n&quot;</span>);</span><br><span class="line">    <span class="keyword">if</span> (mJavaVM-&gt;DetachCurrentThread() != JNI_OK)</span><br><span class="line">        ALOGW(<span class="string">&quot;Warning: unable to detach main thread\n&quot;</span>);</span><br><span class="line">    <span class="keyword">if</span> (mJavaVM-&gt;DestroyJavaVM() != <span class="number">0</span>)</span><br><span class="line">        ALOGW(<span class="string">&quot;Warning: VM did not shut down cleanly\n&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在start()方法中主要完成了：</p>
<ol>
<li>调用startVm开启虚拟机，主要是配置虚拟机的相关参数</li>
<li>调用startReg注册JNI方法</li>
<li>使用JNI把Zyogte进程启动</li>
</ol>
<p>在start()方法的最后用来反射调用了ZygoteInit的main函数，Zygote便进入了Java框架层。<br><strong>进入到ZygoteInit.java#main()</strong><br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteInit.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] argv)</span> &#123;</span><br><span class="line">    <span class="comment">//1.创建ZygoteServer</span></span><br><span class="line">   <span class="type">ZygoteServer</span> <span class="variable">zygoteServer</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"> </span><br><span class="line">   <span class="comment">// Mark zygote start. This ensures that thread creation will throw</span></span><br><span class="line">   <span class="comment">// an error.</span></span><br><span class="line">    <span class="comment">//调用native函数，确保当前没有其它线程在运行</span></span><br><span class="line">   ZygoteHooks.startZygoteNoThreadCreation();</span><br><span class="line"> </span><br><span class="line">   <span class="comment">// Zygote goes into its own process group.</span></span><br><span class="line">    <span class="comment">//设置pid为0，Zygote进入自己的进程组</span></span><br><span class="line">   <span class="keyword">try</span> &#123;</span><br><span class="line">       Os.setpgid(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">   &#125; <span class="keyword">catch</span> (ErrnoException ex) &#123;</span><br><span class="line">       <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;Failed to setpgid(0,0)&quot;</span>, ex);</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">   Runnable caller;</span><br><span class="line">   <span class="keyword">try</span> &#123;</span><br><span class="line">       <span class="comment">// Store now for StatsLogging later.</span></span><br><span class="line">       <span class="keyword">final</span> <span class="type">long</span> <span class="variable">startTime</span> <span class="operator">=</span> SystemClock.elapsedRealtime();</span><br><span class="line">       <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">isRuntimeRestarted</span> <span class="operator">=</span> <span class="string">&quot;1&quot;</span>.equals(</span><br><span class="line">               SystemProperties.get(<span class="string">&quot;sys.boot_completed&quot;</span>));</span><br><span class="line"> </span><br><span class="line">        <span class="comment">//得到systrace的监控TAG</span></span><br><span class="line">       <span class="type">String</span> <span class="variable">bootTimeTag</span> <span class="operator">=</span> Process.is64Bit() ? <span class="string">&quot;Zygote64Timing&quot;</span> : <span class="string">&quot;Zygote32Timing&quot;</span>;</span><br><span class="line">       <span class="type">TimingsTraceLog</span> <span class="variable">bootTimingsTraceLog</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TimingsTraceLog</span>(bootTimeTag,</span><br><span class="line">               Trace.TRACE_TAG_DALVIK);</span><br><span class="line">       <span class="comment">//通过systrace来追踪函数ZygoteInit，可以通过systrace工具来进行分析</span></span><br><span class="line">       <span class="comment">//traceBegin和traceEnd要成对出现，而且需要使用同一个tag</span></span><br><span class="line">       bootTimingsTraceLog.traceBegin(<span class="string">&quot;ZygoteInit&quot;</span>);</span><br><span class="line">       <span class="comment">//开启DDMS(Dalvik Debug Monitor Service)功能</span></span><br><span class="line">       <span class="comment">//注册所有已知的Java VM的处理块的监听器。线程监听、内存监听、native 堆内存监听、debug模式监听等等</span></span><br><span class="line">       RuntimeInit.preForkInit();</span><br><span class="line"> </span><br><span class="line">       <span class="type">boolean</span> <span class="variable">startSystemServer</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">       <span class="type">String</span> <span class="variable">zygoteSocketName</span> <span class="operator">=</span> <span class="string">&quot;zygote&quot;</span>;</span><br><span class="line">       <span class="type">String</span> <span class="variable">abiList</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">       <span class="type">boolean</span> <span class="variable">enableLazyPreload</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line"> </span><br><span class="line">       <span class="comment">//2.解析app_main.cpp#start()传入的参数</span></span><br><span class="line">       <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt; argv.length; i++) &#123;</span><br><span class="line">           <span class="keyword">if</span> (<span class="string">&quot;start-system-server&quot;</span>.equals(argv[i])) &#123;</span><br><span class="line">               startSystemServer = <span class="literal">true</span>;<span class="comment">//启动zygote时，才会传入参数：start-system-server</span></span><br><span class="line">           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">&quot;--enable-lazy-preload&quot;</span>.equals(argv[i])) &#123;</span><br><span class="line">               enableLazyPreload = <span class="literal">true</span>;<span class="comment">//启动zygote_secondary时，才会传入参数：enable-lazy-preload</span></span><br><span class="line">           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (argv[i].startsWith(ABI_LIST_ARG)) &#123;</span><br><span class="line">               abiList = argv[i].substring(ABI_LIST_ARG.length());<span class="comment">//通过属性ro.product.cpu.abilist64\ro.product.cpu.abilist32 从C空间传来的值</span></span><br><span class="line">           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (argv[i].startsWith(SOCKET_NAME_ARG)) &#123;</span><br><span class="line">               zygoteSocketName = argv[i].substring(SOCKET_NAME_ARG.length());<span class="comment">//会有两种值：zygote和zygote_secondary</span></span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">               <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;Unknown command line argument: &quot;</span> + argv[i]);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"> </span><br><span class="line">       <span class="comment">//根据传入socket name来决定是创建zygote还是zygote_secondary</span></span><br><span class="line">       <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">isPrimaryZygote</span> <span class="operator">=</span> zygoteSocketName.equals(Zygote.PRIMARY_SOCKET_NAME);</span><br><span class="line">    </span><br><span class="line">       <span class="keyword">if</span> (!isRuntimeRestarted) &#123;</span><br><span class="line">           <span class="keyword">if</span> (isPrimaryZygote) &#123;</span><br><span class="line">               FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,</span><br><span class="line">                       BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__ZYGOTE_INIT_START,</span><br><span class="line">                       startTime);</span><br><span class="line">           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (zygoteSocketName.equals(Zygote.SECONDARY_SOCKET_NAME)) &#123;</span><br><span class="line">               FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,</span><br><span class="line">                       BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SECONDARY_ZYGOTE_INIT_START,</span><br><span class="line">                       startTime);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"> </span><br><span class="line">       <span class="keyword">if</span> (abiList == <span class="literal">null</span>) &#123;</span><br><span class="line">           <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;No ABI list supplied.&quot;</span>);</span><br><span class="line">       &#125;</span><br><span class="line"> </span><br><span class="line">       <span class="comment">// In some configurations, we avoid preloading resources and classes eagerly.</span></span><br><span class="line">       <span class="comment">// In such cases, we will preload things prior to our first fork.</span></span><br><span class="line">       <span class="comment">// 在第一次zygote启动时，enableLazyPreload为false，执行preload</span></span><br><span class="line">       <span class="keyword">if</span> (!enableLazyPreload) &#123;</span><br><span class="line">           <span class="comment">//systrace 追踪 ZygotePreload</span></span><br><span class="line">           bootTimingsTraceLog.traceBegin(<span class="string">&quot;ZygotePreload&quot;</span>);</span><br><span class="line">           EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,</span><br><span class="line">                   SystemClock.uptimeMillis());</span><br><span class="line">           <span class="comment">/// M: Added for BOOTPROF</span></span><br><span class="line">           addBootEvent(<span class="string">&quot;Zygote:Preload Start&quot;</span>);</span><br><span class="line">           <span class="comment">/// @&#125;</span></span><br><span class="line">           <span class="comment">// 3.加载进程的资源和类</span></span><br><span class="line">           preload(bootTimingsTraceLog);</span><br><span class="line">           EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,</span><br><span class="line">                   SystemClock.uptimeMillis());</span><br><span class="line">           <span class="comment">//systrace结束 ZygotePreload的追踪</span></span><br><span class="line">           bootTimingsTraceLog.traceEnd(); <span class="comment">// ZygotePreload</span></span><br><span class="line">       &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="comment">// Do an initial gc to clean up after startup</span></span><br><span class="line">        bootTimingsTraceLog.traceBegin(<span class="string">&quot;PostZygoteInitGC&quot;</span>);</span><br><span class="line">        gcAndFinalize();</span><br><span class="line">        bootTimingsTraceLog.traceEnd(); <span class="comment">// PostZygoteInitGC</span></span><br><span class="line">       </span><br><span class="line">        <span class="comment">//结束ZygoteInit的systrace追踪</span></span><br><span class="line">        bootTimingsTraceLog.traceEnd(); <span class="comment">// ZygoteInit</span></span><br><span class="line"> </span><br><span class="line">        Zygote.initNativeState(isPrimaryZygote);</span><br><span class="line"> </span><br><span class="line">        <span class="comment">/// M: Added for BOOTPROF</span></span><br><span class="line">        addBootEvent(<span class="string">&quot;Zygote:Preload End&quot;</span>);</span><br><span class="line">        <span class="comment">/// @&#125;</span></span><br><span class="line">        ZygoteHooks.stopZygoteNoThreadCreation();</span><br><span class="line"> </span><br><span class="line">        <span class="comment">// 4.调用ZygoteServer 构造函数，创建socket，会根据传入的参数</span></span><br><span class="line">        <span class="comment">// 创建两个socket：/dev/socket/zygote 和 /dev/socket/zygote_secondary</span></span><br><span class="line">        zygoteServer = <span class="keyword">new</span> <span class="title class_">ZygoteServer</span>(isPrimaryZygote);</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">if</span> (startSystemServer) &#123;</span><br><span class="line">            <span class="comment">//5.fork出SystemServer</span></span><br><span class="line">            <span class="type">Runnable</span> <span class="variable">r</span> <span class="operator">=</span> forkSystemServer(abiList, zygoteSocketName, zygoteServer);</span><br><span class="line"> </span><br><span class="line">            <span class="comment">// &#123;@code r == null&#125; in the parent (zygote) process, and &#123;@code r != null&#125; in the</span></span><br><span class="line">            <span class="comment">// child (system_server) process.</span></span><br><span class="line">            <span class="comment">//启动SystemServer</span></span><br><span class="line">            <span class="keyword">if</span> (r != <span class="literal">null</span>) &#123;</span><br><span class="line">                r.run();</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        Log.i(TAG, <span class="string">&quot;Accepting command socket connections&quot;</span>);</span><br><span class="line"> </span><br><span class="line">        <span class="comment">// The select loop returns early in the child process after a fork and</span></span><br><span class="line">        <span class="comment">// loops forever in the zygote.</span></span><br><span class="line">        <span class="comment">//6.zygote进程进入无限循环，处理请求</span></span><br><span class="line">        caller = zygoteServer.runSelectLoop(abiList);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Throwable ex) &#123;</span><br><span class="line">        Log.e(TAG, <span class="string">&quot;System zygote died with fatal exception&quot;</span>, ex);</span><br><span class="line">        <span class="keyword">throw</span> ex;</span><br><span class="line">    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (zygoteServer != <span class="literal">null</span>) &#123;</span><br><span class="line">            zygoteServer.closeServerSocket();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// We&#x27;re in the child process and have exited the select loop. Proceed to execute the</span></span><br><span class="line">    <span class="comment">// command.</span></span><br><span class="line">    <span class="keyword">if</span> (caller != <span class="literal">null</span>) &#123;</span><br><span class="line">        caller.run();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>总结zygoteInit.java#main()完成了：</p>
<ol>
<li>创建ZygoteServer</li>
<li>解析了从app_main.cpp#start()传入的参数</li>
<li>加载进程的资源和类</li>
<li>调用ZygoteServer构造函数，创建sokect</li>
<li>fork出SystemServer</li>
<li>zygote进程进入无限循环，处理请求</li>
</ol>
<p><strong>进入到ZygoteInit.java#preload()预加载</strong></p>
<p>预加载是指在zygote进程启动的时候就加载，这样系统只在zygote执行一次加载操作，所有APP用到该资源不需要再重新加载，减少资源加载时间，加快了应用启动速度，一般情况下，系统中App共享的资源会被列为预加载资源。<br>zygote fork子进程时，根据fork的copy-on-write(写时拷贝)机制可知，有些类如果不做改变，甚至都不用复制，子进程可以和父进程共享这部分数据，从而省去不少内存的占用。<br>预加载的原理：<br>zygote进程启动后将资源读取出来，保存到Resources一个全局静态变量中，下次读取系统资源的时候优先从静态变量中查找。<br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteInit.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">preload</span><span class="params">(TimingsTraceLog bootTimingsTraceLog)</span> &#123;</span><br><span class="line">    Log.d(TAG, <span class="string">&quot;begin preload&quot;</span>);</span><br><span class="line">    bootTimingsTraceLog.traceBegin(<span class="string">&quot;BeginPreload&quot;</span>);</span><br><span class="line">    beginPreload();<span class="comment">//获取字符集转换资源等</span></span><br><span class="line">    bootTimingsTraceLog.traceEnd(); <span class="comment">// BeginPreload</span></span><br><span class="line">    bootTimingsTraceLog.traceBegin(<span class="string">&quot;PreloadClasses&quot;</span>);</span><br><span class="line">    <span class="comment">//预加载类的列表---/system/etc/preloaded-classes， 在/frameworks/base/config/preloaded-classes 中，Android10.0中预计有7603左右个类</span></span><br><span class="line">    preloadClasses();</span><br><span class="line">    bootTimingsTraceLog.traceEnd(); <span class="comment">// PreloadClasses</span></span><br><span class="line">    bootTimingsTraceLog.traceBegin(<span class="string">&quot;CacheNonBootClasspathClassLoaders&quot;</span>);</span><br><span class="line">    cacheNonBootClasspathClassLoaders();</span><br><span class="line">    bootTimingsTraceLog.traceEnd(); <span class="comment">// CacheNonBootClasspathClassLoaders</span></span><br><span class="line">    bootTimingsTraceLog.traceBegin(<span class="string">&quot;PreloadResources&quot;</span>);</span><br><span class="line">    <span class="comment">//加载图片、颜色等资源文件，部分定义在 /frameworks/base/core/res/res/values/arrays.xml中</span></span><br><span class="line">    preloadResources();</span><br><span class="line">    bootTimingsTraceLog.traceEnd(); <span class="comment">// PreloadResources</span></span><br><span class="line">    Trace.traceBegin(Trace.TRACE_TAG_DALVIK, <span class="string">&quot;PreloadAppProcessHALs&quot;</span>);</span><br><span class="line">    nativePreloadAppProcessHALs();</span><br><span class="line">    Trace.traceEnd(Trace.TRACE_TAG_DALVIK);</span><br><span class="line">    Trace.traceBegin(Trace.TRACE_TAG_DALVIK, <span class="string">&quot;PreloadGraphicsDriver&quot;</span>);</span><br><span class="line">    maybePreloadGraphicsDriver();</span><br><span class="line">    Trace.traceEnd(Trace.TRACE_TAG_DALVIK);</span><br><span class="line">    <span class="comment">// 加载 android、compiler_rt、jnigraphics等library</span></span><br><span class="line">    preloadSharedLibraries();</span><br><span class="line">    <span class="comment">//用于初始化文字资源</span></span><br><span class="line">    preloadTextResources();</span><br><span class="line">    <span class="comment">// Ask the WebViewFactory to do any initialization that must run in the zygote process,</span></span><br><span class="line">    <span class="comment">// for memory sharing purposes.</span></span><br><span class="line">    <span class="comment">//用于初始化webview;</span></span><br><span class="line">    WebViewFactory.prepareWebViewInZygote();</span><br><span class="line">    <span class="comment">//预加载完成，可以查看下面的log</span></span><br><span class="line">    endPreload();</span><br><span class="line">    warmUpJcaProviders();</span><br><span class="line">    Log.d(TAG, <span class="string">&quot;end preload&quot;</span>);</span><br><span class="line"> </span><br><span class="line">    sPreloadComplete = <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>进入到ZygoteServer.java#ZygoteServer(boolean isPrimaryZygote)<br>说明：ZygoteServer 构造函数初始化时，根据传入的参数，利用LocalServerSocket （createManagedSocketFromInitSocket()方法中调用）创建了1个本地服务端的socket，用来建立连接。<br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteServer.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建zygote的socket</span></span><br><span class="line">ZygoteServer(<span class="type">boolean</span> isPrimaryZygote) &#123;</span><br><span class="line">    mUsapPoolEventFD = Zygote.getUsapPoolEventFD();</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">if</span> (isPrimaryZygote) &#123;</span><br><span class="line">        <span class="comment">//创建socket，并获取socket对象，socketname：zygote</span></span><br><span class="line">        mZygoteSocket = Zygote.createManagedSocketFromInitSocket(Zygote.PRIMARY_SOCKET_NAME);</span><br><span class="line">        mUsapPoolSocket =</span><br><span class="line">                Zygote.createManagedSocketFromInitSocket(</span><br><span class="line">                        Zygote.USAP_POOL_PRIMARY_SOCKET_NAME);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//socketname：zygote_secondary</span></span><br><span class="line">        mZygoteSocket = Zygote.createManagedSocketFromInitSocket(Zygote.SECONDARY_SOCKET_NAME);</span><br><span class="line">        mUsapPoolSocket =</span><br><span class="line">                Zygote.createManagedSocketFromInitSocket(</span><br><span class="line">                        Zygote.USAP_POOL_SECONDARY_SOCKET_NAME);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    mUsapPoolSupported = <span class="literal">true</span>;</span><br><span class="line">    fetchUsapPoolPolicyProps();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>进入到ZygoteInit.java#forkSystemServer()<br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteInit.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> Runnable <span class="title function_">forkSystemServer</span><span class="params">(String abiList, String socketName,</span></span><br><span class="line"><span class="params">        ZygoteServer zygoteServer)</span> &#123;</span><br><span class="line">    <span class="type">long</span> <span class="variable">capabilities</span> <span class="operator">=</span> posixCapabilitiesAsBits(</span><br><span class="line">            OsConstants.CAP_IPC_LOCK,</span><br><span class="line">            OsConstants.CAP_KILL,</span><br><span class="line">            OsConstants.CAP_NET_ADMIN,</span><br><span class="line">            OsConstants.CAP_NET_BIND_SERVICE,</span><br><span class="line">            OsConstants.CAP_NET_BROADCAST,</span><br><span class="line">            OsConstants.CAP_NET_RAW,</span><br><span class="line">            OsConstants.CAP_SYS_MODULE,</span><br><span class="line">            OsConstants.CAP_SYS_NICE,</span><br><span class="line">            OsConstants.CAP_SYS_PTRACE,</span><br><span class="line">            OsConstants.CAP_SYS_TIME,</span><br><span class="line">            OsConstants.CAP_SYS_TTY_CONFIG,</span><br><span class="line">            OsConstants.CAP_WAKE_ALARM,</span><br><span class="line">            OsConstants.CAP_BLOCK_SUSPEND</span><br><span class="line">    );</span><br><span class="line">    ....</span><br><span class="line">    <span class="comment">//准备参数</span></span><br><span class="line">    <span class="comment">/* Hardcoded command line to start the system server */</span></span><br><span class="line">    String[] args = &#123;</span><br><span class="line">            <span class="string">&quot;--setuid=1000&quot;</span>,</span><br><span class="line">            <span class="string">&quot;--setgid=1000&quot;</span>,</span><br><span class="line">            <span class="string">&quot;--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,&quot;</span></span><br><span class="line">                    + <span class="string">&quot;1024,1032,1065,3001,3002,3003,3006,3007,3009,3010,3011&quot;</span>,</span><br><span class="line">            <span class="string">&quot;--capabilities=&quot;</span> + capabilities + <span class="string">&quot;,&quot;</span> + capabilities,</span><br><span class="line">            <span class="string">&quot;--nice-name=system_server&quot;</span>,</span><br><span class="line">            <span class="string">&quot;--runtime-args&quot;</span>,</span><br><span class="line">            <span class="string">&quot;--target-sdk-version=&quot;</span> + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT,</span><br><span class="line">            <span class="string">&quot;com.android.server.SystemServer&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    ZygoteArguments parsedArgs;</span><br><span class="line"> </span><br><span class="line">    <span class="type">int</span> pid;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="type">ZygoteCommandBuffer</span> <span class="variable">commandBuffer</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ZygoteCommandBuffer</span>(args);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//将上面准备的参数，按照ZygoteArguments的风格进行封装</span></span><br><span class="line">            parsedArgs = ZygoteArguments.getInstance(commandBuffer);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (EOFException e) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">AssertionError</span>(<span class="string">&quot;Unexpected argument error for forking system server&quot;</span>, e);</span><br><span class="line">        &#125;</span><br><span class="line">        commandBuffer.close();</span><br><span class="line">        Zygote.applyDebuggerSystemProperty(parsedArgs);</span><br><span class="line">        Zygote.applyInvokeWithSystemProperty(parsedArgs);</span><br><span class="line"> </span><br><span class="line">    .....</span><br><span class="line">        <span class="comment">//通过fork“分裂”出子进程system_server</span></span><br><span class="line">        <span class="comment">/* Request to fork the system server process */</span></span><br><span class="line">        pid = Zygote.forkSystemServer(</span><br><span class="line">                parsedArgs.mUid, parsedArgs.mGid,</span><br><span class="line">                parsedArgs.mGids,</span><br><span class="line">                parsedArgs.mRuntimeFlags,</span><br><span class="line">                <span class="literal">null</span>,</span><br><span class="line">                parsedArgs.mPermittedCapabilities,</span><br><span class="line">                parsedArgs.mEffectiveCapabilities);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IllegalArgumentException ex) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(ex);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">//进入子进程system_server</span></span><br><span class="line">    <span class="comment">/* For child process */</span></span><br><span class="line">    <span class="keyword">if</span> (pid == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// 处理32_64和64_32的情况</span></span><br><span class="line">        <span class="keyword">if</span> (hasSecondZygote(abiList)) &#123;</span><br><span class="line">            waitForSecondaryZygote(socketName);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// fork时会copy socket，system server需要主动关闭</span></span><br><span class="line">        zygoteServer.closeServerSocket();</span><br><span class="line">        <span class="comment">// system server进程处理自己的工作</span></span><br><span class="line">        <span class="keyword">return</span> handleSystemServerProcess(parsedArgs);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>主要时调用了Zygote.java#forkSystemServer()方法，在这个方法中调用了Native的方法，但最后会调用fork()方法创建子进程。<br>fork()函数得到的子进程是父进程的一个复制品，通过写时拷贝实现。<br>fork调用的一个奇妙之处就是它仅仅被调用一次，却能够返回两次。<br>返回值：</p>
<ul>
<li>返回0：表示成功创建子进程，并且接下来进入子进程执行liuc</li>
<li>返回&gt;0：表示成功创建子进程，并且急促执行父进程流程代码</li>
<li>返回非正数&lt;0：表示创建子进程失败，失败的原因主要有：进程数超过了系统所能创建的上线，errno会被设置为EAGAIN系统内存不足，errno会被设置为ENOMEM</li>
</ul>
<p>回到ZygoteServer()方法中，在新fork出的子进程中调用了handleSystemServerProcess()，主要是返回Runtime.java的MethodAndArgsCaller的方法，然后通过r.run() 启动com.android.server.SystemServer的main 方法。<br>这个当我们后面的SystemServer的章节进行详细讲解。<br><strong>handleSystemServerProcess代码流程：</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">handleSystemServerProcess()</span><br><span class="line">    |</span><br><span class="line">    [ZygoteInit.java]</span><br><span class="line">    zygoteInit()</span><br><span class="line">        |</span><br><span class="line">    [RuntimeInit.java]</span><br><span class="line">    applicationInit()</span><br><span class="line">        |</span><br><span class="line">    findStaticMain()</span><br><span class="line">        |</span><br><span class="line">    MethodAndArgsCaller()</span><br></pre></td></tr></table></figure>
<p><strong>进入ZygoteServer.java#runSelectLoop()</strong><br>说明：使zygote进程进入无限循环，处理请求<br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteServer.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br></pre></td><td class="code"><pre><span class="line">Runnable <span class="title function_">runSelectLoop</span><span class="params">(String abiList)</span> &#123;</span><br><span class="line">    ArrayList&lt;FileDescriptor&gt; socketFDs = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">    ArrayList&lt;ZygoteConnection&gt; peers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"> </span><br><span class="line">    <span class="comment">//将server socket加入到fds</span></span><br><span class="line">    socketFDs.add(mZygoteSocket.getFileDescriptor());</span><br><span class="line">    peers.add(<span class="literal">null</span>);</span><br><span class="line"> </span><br><span class="line">    mUsapPoolRefillTriggerTimestamp = INVALID_TIMESTAMP;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        fetchUsapPoolPolicyPropsWithMinInterval();</span><br><span class="line">        mUsapPoolRefillAction = UsapPoolRefillAction.NONE;</span><br><span class="line"> </span><br><span class="line">        <span class="type">int</span>[] usapPipeFDs = <span class="literal">null</span>;</span><br><span class="line">        StructPollfd[] pollFDs;</span><br><span class="line">        <span class="comment">//每次循环，都重新创建需要监听的pollFds</span></span><br><span class="line">        <span class="keyword">if</span> (mUsapPoolEnabled) &#123;</span><br><span class="line">            usapPipeFDs = Zygote.getUsapPipeFDs();</span><br><span class="line">            pollFDs = <span class="keyword">new</span> <span class="title class_">StructPollfd</span>[socketFDs.size() + <span class="number">1</span> + usapPipeFDs.length];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            pollFDs = <span class="keyword">new</span> <span class="title class_">StructPollfd</span>[socketFDs.size()];</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="type">int</span> <span class="variable">pollIndex</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (FileDescriptor socketFD : socketFDs) &#123;</span><br><span class="line">            <span class="comment">//关注事件到来</span></span><br><span class="line">            pollFDs[pollIndex] = <span class="keyword">new</span> <span class="title class_">StructPollfd</span>();</span><br><span class="line">            pollFDs[pollIndex].fd = socketFD;</span><br><span class="line">            pollFDs[pollIndex].events = (<span class="type">short</span>) POLLIN;</span><br><span class="line">            ++pollIndex;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> <span class="variable">usapPoolEventFDIndex</span> <span class="operator">=</span> pollIndex;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">if</span> (mUsapPoolEnabled) &#123;</span><br><span class="line">            pollFDs[pollIndex] = <span class="keyword">new</span> <span class="title class_">StructPollfd</span>();</span><br><span class="line">            pollFDs[pollIndex].fd = mUsapPoolEventFD;</span><br><span class="line">            pollFDs[pollIndex].events = (<span class="type">short</span>) POLLIN;</span><br><span class="line">            ++pollIndex;</span><br><span class="line"> </span><br><span class="line">            <span class="comment">// The usapPipeFDs array will always be filled in if the USAP Pool is enabled.</span></span><br><span class="line">            <span class="keyword">assert</span> usapPipeFDs != <span class="literal">null</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> usapPipeFD : usapPipeFDs) &#123;</span><br><span class="line">                <span class="type">FileDescriptor</span> <span class="variable">managedFd</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileDescriptor</span>();</span><br><span class="line">                managedFd.setInt$(usapPipeFD);</span><br><span class="line"> </span><br><span class="line">                pollFDs[pollIndex] = <span class="keyword">new</span> <span class="title class_">StructPollfd</span>();</span><br><span class="line">                pollFDs[pollIndex].fd = managedFd;</span><br><span class="line">                pollFDs[pollIndex].events = (<span class="type">short</span>) POLLIN;</span><br><span class="line">                ++pollIndex;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="type">int</span> pollTimeoutMs;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">if</span> (mUsapPoolRefillTriggerTimestamp == INVALID_TIMESTAMP) &#123;</span><br><span class="line">            pollTimeoutMs = -<span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="type">long</span> <span class="variable">elapsedTimeMs</span> <span class="operator">=</span> System.currentTimeMillis() - mUsapPoolRefillTriggerTimestamp;</span><br><span class="line"> </span><br><span class="line">            <span class="keyword">if</span> (elapsedTimeMs &gt;= mUsapPoolRefillDelayMs) &#123;</span><br><span class="line">                <span class="comment">// The refill delay has elapsed during the period between poll invocations.</span></span><br><span class="line">                <span class="comment">// We will now check for any currently ready file descriptors before refilling</span></span><br><span class="line">                <span class="comment">// the USAP pool.</span></span><br><span class="line">                pollTimeoutMs = <span class="number">0</span>;</span><br><span class="line">                mUsapPoolRefillTriggerTimestamp = INVALID_TIMESTAMP;</span><br><span class="line">                mUsapPoolRefillAction = UsapPoolRefillAction.DELAYED;</span><br><span class="line"> </span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (elapsedTimeMs &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="comment">// This can occur if the clock used by currentTimeMillis is reset, which is</span></span><br><span class="line">                <span class="comment">// possible because it is not guaranteed to be monotonic.  Because we can&#x27;t tell</span></span><br><span class="line">                <span class="comment">// how far back the clock was set the best way to recover is to simply re-start</span></span><br><span class="line">                <span class="comment">// the respawn delay countdown.</span></span><br><span class="line">                pollTimeoutMs = mUsapPoolRefillDelayMs;</span><br><span class="line"> </span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                pollTimeoutMs = (<span class="type">int</span>) (mUsapPoolRefillDelayMs - elapsedTimeMs);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="type">int</span> pollReturnValue;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//等待事件到来</span></span><br><span class="line">            pollReturnValue = Os.poll(pollFDs, pollTimeoutMs);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ErrnoException ex) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;poll failed&quot;</span>, ex);</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">if</span> (pollReturnValue == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">// The poll returned zero results either when the timeout value has been exceeded</span></span><br><span class="line">            <span class="comment">// or when a non-blocking poll is issued and no FDs are ready.  In either case it</span></span><br><span class="line">            <span class="comment">// is time to refill the pool.  This will result in a duplicate assignment when</span></span><br><span class="line">            <span class="comment">// the non-blocking poll returns zero results, but it avoids an additional</span></span><br><span class="line">            <span class="comment">// conditional in the else branch.</span></span><br><span class="line">            mUsapPoolRefillTriggerTimestamp = INVALID_TIMESTAMP;</span><br><span class="line">            mUsapPoolRefillAction = UsapPoolRefillAction.DELAYED;</span><br><span class="line"> </span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="type">boolean</span> <span class="variable">usapPoolFDRead</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="comment">//倒序处理，即优先处理已建立链接的信息，后处理新建链接的请求</span></span><br><span class="line">            <span class="keyword">while</span> (--pollIndex &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> ((pollFDs[pollIndex].revents &amp; POLLIN) == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line"> </span><br><span class="line">                <span class="comment">//server socket最先加入fds，因此这里是server socket收到数据</span></span><br><span class="line">                <span class="keyword">if</span> (pollIndex == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="comment">// Zygote server socket</span></span><br><span class="line">                    <span class="comment">//收到新的建立通信的请求，建立通信连接</span></span><br><span class="line">                    <span class="type">ZygoteConnection</span> <span class="variable">newPeer</span> <span class="operator">=</span> acceptCommandPeer(abiList);</span><br><span class="line">                    <span class="comment">//加入到peers和fds，即下一次也开监听</span></span><br><span class="line">                    peers.add(newPeer);</span><br><span class="line">                    socketFDs.add(newPeer.getFileDescriptor());</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (pollIndex &lt; usapPoolEventFDIndex) &#123;</span><br><span class="line">                    <span class="comment">// Session socket accepted from the Zygote server socket</span></span><br><span class="line">                    <span class="comment">//说明接收AMS发送过来创建应用程序的请求，调用processOneCommand来创建新的应用程序进程</span></span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        <span class="comment">//有socket连接，创建ZygoteConnection对象，并添加到fds</span></span><br><span class="line">                        <span class="type">ZygoteConnection</span> <span class="variable">connection</span> <span class="operator">=</span> peers.get(pollIndex);</span><br><span class="line">                        <span class="type">boolean</span> <span class="variable">multipleForksOK</span> <span class="operator">=</span> !isUsapPoolEnabled()</span><br><span class="line">                                &amp;&amp; ZygoteHooks.isIndefiniteThreadSuspensionSafe();</span><br><span class="line">                        <span class="comment">//处理连接</span></span><br><span class="line">                        <span class="keyword">final</span> <span class="type">Runnable</span> <span class="variable">command</span> <span class="operator">=</span></span><br><span class="line">                                connection.processCommand(<span class="built_in">this</span>, multipleForksOK);</span><br><span class="line"> </span><br><span class="line">                        <span class="comment">// TODO (chriswailes): Is this extra check necessary?</span></span><br><span class="line">                        <span class="keyword">if</span> (mIsForkChild) &#123;</span><br><span class="line">                            <span class="comment">// We&#x27;re in the child. We should always have a command to run at</span></span><br><span class="line">                            <span class="comment">// this stage if processCommand hasn&#x27;t called &quot;exec&quot;.</span></span><br><span class="line">                            <span class="keyword">if</span> (command == <span class="literal">null</span>) &#123;</span><br><span class="line">                                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(<span class="string">&quot;command == null&quot;</span>);</span><br><span class="line">                            &#125;</span><br><span class="line"> </span><br><span class="line">                            <span class="keyword">return</span> command;</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            <span class="comment">// We&#x27;re in the server - we should never have any commands to run.</span></span><br><span class="line">                            <span class="keyword">if</span> (command != <span class="literal">null</span>) &#123;</span><br><span class="line">                                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(<span class="string">&quot;command != null&quot;</span>);</span><br><span class="line">                            &#125;</span><br><span class="line"> </span><br><span class="line">                            <span class="comment">// We don&#x27;t know whether the remote side of the socket was closed or</span></span><br><span class="line">                            <span class="comment">// not until we attempt to read from it from processCommand. This</span></span><br><span class="line">                            <span class="comment">// shows up as a regular POLLIN event in our regular processing</span></span><br><span class="line">                            <span class="comment">// loop.</span></span><br><span class="line">                            <span class="keyword">if</span> (connection.isClosedByPeer()) &#123;</span><br><span class="line">                                connection.closeSocket();</span><br><span class="line">                                peers.remove(pollIndex);</span><br><span class="line">                                socketFDs.remove(pollIndex);<span class="comment">//处理完则从fds中移除该文件描述符</span></span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                        ......</span><br><span class="line">                    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                        <span class="comment">// Reset the child flag, in the event that the child process is a child-</span></span><br><span class="line">                        <span class="comment">// zygote. The flag will not be consulted this loop pass after the</span></span><br><span class="line">                        <span class="comment">// Runnable is returned.</span></span><br><span class="line">                        mIsForkChild = <span class="literal">false</span>;</span><br><span class="line">                    &#125;</span><br><span class="line"> </span><br><span class="line">                &#125;</span><br><span class="line">                    ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>processOneCommand()<br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">Runnable <span class="title function_">processCommand</span><span class="params">(ZygoteServer zygoteServer, <span class="type">boolean</span> multipleOK)</span> &#123;</span><br><span class="line">.....</span><br><span class="line">            <span class="keyword">if</span> (parsedArgs.mInvokeWith != <span class="literal">null</span> || parsedArgs.mStartChildZygote</span><br><span class="line">                    || !multipleOK || peer.getUid() != Process.SYSTEM_UID) &#123;</span><br><span class="line">                <span class="comment">// Continue using old code for now. <span class="doctag">TODO:</span> Handle these cases in the other path.</span></span><br><span class="line">                <span class="comment">//fork子进程</span></span><br><span class="line">                pid = Zygote.forkAndSpecialize(parsedArgs.mUid, parsedArgs.mGid,</span><br><span class="line">                        parsedArgs.mGids, parsedArgs.mRuntimeFlags, rlimits,</span><br><span class="line">                        parsedArgs.mMountExternal, parsedArgs.mSeInfo, parsedArgs.mNiceName,</span><br><span class="line">                        fdsToClose, fdsToIgnore, parsedArgs.mStartChildZygote,</span><br><span class="line">                        parsedArgs.mInstructionSet, parsedArgs.mAppDataDir,</span><br><span class="line">                        parsedArgs.mIsTopApp, parsedArgs.mPkgDataInfoList,</span><br><span class="line">                        parsedArgs.mAllowlistedDataInfoList, parsedArgs.mBindMountAppDataDirs,</span><br><span class="line">                        parsedArgs.mBindMountAppStorageDirs);</span><br><span class="line"> </span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (pid == <span class="number">0</span>) &#123;</span><br><span class="line">                        <span class="comment">// in child</span></span><br><span class="line">                        <span class="comment">//子进程执行</span></span><br><span class="line">                        zygoteServer.setForkChild();</span><br><span class="line">    </span><br><span class="line">                        zygoteServer.closeServerSocket();</span><br><span class="line">                        IoUtils.closeQuietly(serverPipeFd);</span><br><span class="line">                        serverPipeFd = <span class="literal">null</span>;</span><br><span class="line">                        <span class="comment">//进入子进程流程</span></span><br><span class="line">                        <span class="keyword">return</span> handleChildProc(parsedArgs, childPipeFd,</span><br><span class="line">                                parsedArgs.mStartChildZygote);</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="comment">// In the parent. A pid &lt; 0 indicates a failure and will be handled in</span></span><br><span class="line">                        <span class="comment">// handleParentProc.</span></span><br><span class="line">                        <span class="comment">//父进程执行</span></span><br><span class="line">                        IoUtils.closeQuietly(childPipeFd);</span><br><span class="line">                        childPipeFd = <span class="literal">null</span>;</span><br><span class="line">                        handleParentProc(pid, serverPipeFd);</span><br><span class="line">                        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        .....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>handleChildProc()<br><strong>frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> Runnable <span class="title function_">handleChildProc</span><span class="params">(ZygoteArguments parsedArgs,</span></span><br><span class="line"><span class="params">        FileDescriptor pipeFd, <span class="type">boolean</span> isZygote)</span> &#123;</span><br><span class="line"> </span><br><span class="line">    closeSocket();</span><br><span class="line"> </span><br><span class="line">    Zygote.setAppProcessName(parsedArgs, TAG);</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// End of the postFork event.</span></span><br><span class="line">    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);</span><br><span class="line">    <span class="keyword">if</span> (parsedArgs.mInvokeWith != <span class="literal">null</span>) &#123;</span><br><span class="line">        WrapperInit.execApplication(parsedArgs.mInvokeWith,</span><br><span class="line">                parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion,</span><br><span class="line">                VMRuntime.getCurrentInstructionSet(),</span><br><span class="line">                pipeFd, parsedArgs.mRemainingArgs);</span><br><span class="line"> </span><br><span class="line">        <span class="comment">// Should not get here.</span></span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(<span class="string">&quot;WrapperInit.execApplication unexpectedly returned&quot;</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!isZygote) &#123;</span><br><span class="line">            <span class="comment">//App进程将会调用到这里，执行目标类的main()方法</span></span><br><span class="line">            <span class="keyword">return</span> ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,</span><br><span class="line">                    parsedArgs.mDisabledCompatChanges,</span><br><span class="line">                    parsedArgs.mRemainingArgs, <span class="literal">null</span> <span class="comment">/* classLoader */</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> ZygoteInit.childZygoteInit(</span><br><span class="line">                    parsedArgs.mRemainingArgs  <span class="comment">/* classLoader */</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p><img src="/images/20240302/20240302001.png"></p>
<ol>
<li>开启虚拟器</li>
<li>注册了JNI</li>
<li>通过JNI调用ZygoteInit的main函数进入Zygote的Java框架层</li>
<li>创建了服务端Socket</li>
<li>预加载类和资源</li>
<li>启动SystemServer进程</li>
<li>通过runSelectLoop函数无限循环等待如AMS等的请求</li>
</ol>

      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2024/03/02/2024-03-02/">http://longzhiye.top/2024/03/02/2024-03-02/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Linux/" rel="tag">Linux</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2024/03/03/2024-03-03/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android.mk变量解析'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2024/02/25/2024-02-25/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 14 init进程解析'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
